home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / ggraph / points.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-13  |  7.2 KB  |  253 lines

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <strings.h>
  4. #include <math.h>
  5. #include "ggraph.h"
  6. #include "ggraphdefs.h"
  7.  
  8. /****************************************************************
  9.  *                                *
  10.  *    plotpoints - write the points of a line            *
  11.  *                                *
  12.  ****************************************************************/
  13. plotpoints () {
  14.     register int    i;
  15.     register float   llabxl, llabyl;
  16.     register float oscalex, oscaley;
  17.     int clipped = 0;
  18.     
  19.     for (curline = 0; ((curline != cg.maxlines) && (cl != NULL)); ++curline) {
  20.     if (cl->lonoff) {
  21.         if (symbsw) {
  22.         for (i = 0; i < cl->maxpoint; i++) {
  23.         /* put a circle around the point */
  24.             if(cg.logxsw){/* reset scale for logs */
  25.               oscalex = cg.scalex;/* save old scale */
  26.               cg.scalex = (XPLOTMAX - cg.xorigin) /
  27.             cg.numtickx;
  28.               graphx = ((( (float) (log10((double)cl->xpoints[i])/
  29.             log10((double)cg.logxtick)))
  30.              - cg.xoffset) * cg.scalex) + cg.xorigin;
  31.               cg.scalex = oscalex;
  32.             }else
  33.               graphx = ((cl->xpoints[i] - cg.xoffset) * cg.scalex)
  34.             + cg.xorigin;
  35.             if(cg.logysw){
  36.               oscaley = cg.scaley;
  37.               cg.scaley = (YPLOTMAX - cg.yorigin) / 
  38.             cg.numticky;
  39.               graphy = (((float)(log10((double)cl->ypoints[i])/
  40.             log10((double)cg.logytick))
  41.              - cg.yoffset) * cg.scaley) + cg.yorigin;
  42.               cg.scaley = oscaley;
  43.             }else
  44.               graphy = ((cl->ypoints[i] - cg.yoffset) * cg.scaley)
  45.             + cg.yorigin;
  46.             if (!((graphy < 0.0) || (graphy > 512.0) ||
  47.                 (graphx < 0.0) || (graphx > 512.0))){
  48.                 draw_symbol(cl->mtype, graphx, graphy);
  49.             }else
  50.             fprintf (stderr,
  51.             "%s: Symbol point out of range x %f y %f\npoint %f %f\n", 
  52. graphname, graphx, graphy, cl->xpoints[i], cl->ypoints[i]);
  53.         }
  54.         }
  55.     /* connect the dots */
  56.         if (cl->ctype) {
  57.         if(version == SUN_GREMLIN)
  58.           fprintf (outfile, "%s\n",
  59.               (cl->ctype == LINE) ? "VECTOR" : "CURVE");
  60.         else
  61.           fprintf (outfile, "%d\n", cl->ctype);
  62.         for (i = 0; i < cl->maxpoint; i++) {
  63.             if(cg.logxsw){
  64.               oscalex = cg.scalex;
  65.               cg.scalex = (XPLOTMAX - cg.xorigin) /
  66.              cg.numtickx;
  67.               graphx = (((float)(log10((double)cl->xpoints[i])/
  68.             log10((double)cg.logxtick))
  69.              - cg.xoffset) * cg.scalex) + cg.xorigin;
  70.               cg.scalex = oscalex;
  71.             }else
  72.               graphx = ((cl->xpoints[i] - cg.xoffset) * cg.scalex)
  73.             + cg.xorigin;
  74.             if(cg.logysw){
  75.               oscaley = cg.scaley;
  76.               cg.scaley = (YPLOTMAX - cg.yorigin) / 
  77.             cg.numticky;
  78. /* printf("point %f partial %f off %f scale %f\n",
  79.  cl->ypoints[i],(float)(log10((double)cl->ypoints[i])/
  80.   log10((double)cg.logytick)),cg.yoffset,cg.scaley); */
  81.               graphy = (((float)(log10((double)cl->ypoints[i])/
  82.             log10((double)cg.logytick))
  83.              - cg.yoffset) * cg.scaley) + cg.yorigin;
  84.               cg.scaley = oscaley;
  85.             }else
  86.               graphy = ((cl->ypoints[i] - cg.yoffset) * cg.scaley)
  87.             + cg.yorigin;
  88.             if ((graphy < 0.0) || (graphy > 512.0) ||
  89.                 (graphx < 0.0) || (graphx > 512.0)) {
  90.                 fprintf (stderr,
  91.             "%s: Line point out of range x %f y %f\npoint %f %f\n", 
  92.             graphname, graphx, graphy, cl->xpoints[i],
  93.             cl->ypoints[i]);
  94.                 if (!clipped) {
  95.                     fprintf(outfile, 
  96.                        (version == SUN_GREMLIN) ?
  97.                        "*\n" : "-1.00 -1.00\n");
  98.                     fprintf (outfile, "%d %d\n%d\n",
  99.                         cl->ltype, 0, 0);
  100.                     clipped = 1;
  101.                 } 
  102.             } else {
  103.                 if (clipped) {
  104.                     fprintf (outfile, "%d\n", cl->ctype);
  105.                     clipped = 0;
  106.                 } 
  107.             fprintf (outfile, "%4.1f %4.1f\n", graphx, graphy);
  108. /*            if(ddebug)printf ("point %4.1f %4.1f\n",
  109.                            graphx, graphy);*/
  110.             }
  111.         }
  112.         fprintf(outfile, (version == SUN_GREMLIN) ? "*\n" : "-1.00 -1.00\n");
  113.         fprintf (outfile, "%d %d\n%d\n", cl->ltype, 0, 0);
  114.         }
  115.         if (cl->llabsw) {
  116.         if (cl->llabel.t_text[0] == NULL)
  117.             strcpy (cl->llabel.t_text, cl->lname);
  118.         if (!cl->llabel.t_xpos)
  119.             if(cg.logxsw){
  120.               oscalex = cg.scalex;
  121.               cg.scalex = (XPLOTMAX - cg.xorigin) / 
  122.             cg.numtickx;
  123.               llabxl = (((float)(log10((double)cl->xpoints[cl->maxpoint-1])/
  124.             log10((double)cg.logxtick))
  125.              - cg.xoffset) * cg.scalex) + cg.xorigin;
  126.               cg.scalex = oscalex;
  127.             }else
  128.             llabxl = ((cl->xpoints[cl->maxpoint - 1] -cg.xoffset) * cg.scalex)
  129.             + cg.xorigin+5.0;
  130.         else
  131.             llabxl = (cl->llabel.t_xpos * cg.scalex) + cg.xorigin;
  132.         if (!cl->llabel.t_ypos)
  133.             if(cg.logysw){
  134.               oscaley = cg.scaley;
  135.               cg.scaley = (YPLOTMAX - cg.yorigin) /
  136.              cg.numticky;
  137.               llabyl = (((float)(log10((double)cl->ypoints[cl->maxpoint-1])/
  138.             log10((double)cg.logytick))
  139.              - cg.yoffset) * cg.scaley) + cg.yorigin;
  140.               cg.scaley = oscaley;
  141.             }else
  142.             llabyl = ((cl->ypoints[cl->maxpoint - 1] - cg.yoffset)
  143.               * cg.scaley) + cg.yorigin + 5.0;
  144.         else
  145.             llabyl = ((cl->llabel.t_ypos - cg.yoffset) * cg.scaley) + 
  146.             cg.yorigin;
  147. if(debug)printf("labels %f %f point %f %f high %d scale %f\n", llabxl, llabyl, 
  148.  cl->xpoints[cl->maxpoint -1],cl->ypoints[cl->maxpoint -1], cl->maxpoint, 
  149. cg.scalex);
  150.         drawctext (llabxl, llabyl, cl->llabel.t_font, 
  151.           cl->llabel.t_size, cl->llabel.t_text,
  152.             CENTERLEFT_TEXT);
  153.         }
  154.     }
  155.     }
  156. }
  157. /****************************************************************
  158.  *                                *
  159.  *    readpoints - read the points in             *
  160.  *            returns point number            *
  161.  *                                *
  162.  ****************************************************************/
  163. int     readpoints (infile, line_name)
  164. FILE *infile;
  165. char   *line_name;
  166. {
  167.     int     count;
  168.     register int    i;
  169.     register int    done;
  170.     float   x,
  171.             y;
  172.     float   hix,
  173.             hiy,
  174.             lox,
  175.             loy;
  176.     char    iline[80];
  177.     char   *fgets ();
  178.  
  179.     count = 0;
  180.     while (count != -1) {    /* wait for EOF */
  181.     count = 0;
  182.     done = FALSE;
  183.     hix = hiy = 0.0;
  184.     lox = loy = HUGE;
  185.     curline++;
  186.                 /* allocate a line structure */
  187.     cl = (struct aline *)malloc(sizeof(struct aline));
  188.     cl->forw_line = NULL;    /* link chain */
  189.     cl->back_line = NULL;
  190.     cg.lines[curline+1] = NULL;/* terminate chain */
  191.     cl->mtype = -1;
  192.     cl->ltype = -1;
  193.     cl->ctype = -1;
  194.     cl->llabel.t_size = -1;
  195.     cl->llabel.t_font = -1;
  196.     cl->llelabel.t_size = -1;
  197.     cl->llelabel.t_font = -1;
  198.     strcpy (cl->lname, line_name);
  199.     cl->llabel.t_size = -1;
  200.     cl->llabel.t_font = -1;
  201.     cl->llabel.t_xpos = 0.0;
  202.     cl->llabel.t_ypos = 0.0;
  203.     cl->lonoff = 1;
  204.     cl->ctype = -1;
  205.     cl->mtype = -1;
  206.     cl->ltype = -1;
  207.     while (!done) {        /* loop around for a while */
  208.         if (fgets (iline, 80, infile) == NULL) {/* read numbers */
  209.         done = TRUE;    /* EOF stop reading */
  210.         count = -1;    /* let reader know */
  211.         }
  212.         else
  213.         if (strncmp ("dae", iline, 3) == 0) {
  214.         /* End-of-line-segment */
  215.             done = TRUE;
  216.         }
  217.         else {        /* calculate point on graph */
  218.             sscanf (iline, "%f %f", &x, &y);
  219.             if (x > hix)
  220.             hix = x;
  221.             if (x < lox)
  222.             lox = x;
  223.             if (y > hiy)
  224.             hiy = y;
  225.             if (y < loy)
  226.             loy = y;
  227.             cl->xpoints[count] = x;
  228.             cl->ypoints[count] = y;
  229.             count++;
  230.         }
  231.     }
  232.     /* set count of number of points */
  233.     cl->maxpoint = count;
  234.     cl->minx = lox;
  235.     cl->miny = loy;
  236.     cl->maxx = hix;
  237.     cl->maxy = hiy;
  238.     cg.xoffset = lox;
  239.     cg.yoffset = loy;
  240.     if (cg.gminx > lox)
  241.         cg.gminx = cl->minx;
  242.     if (cg.gminy > loy)
  243.         cg.gminy = cl->miny;
  244.     if (cg.gmaxx < hix)
  245.         cg.gmaxx = cl->maxx;
  246.     if (cg.gmaxy < hiy)
  247.         cg.gmaxy = cl->maxy;
  248.  
  249.     return (curline + 1);    /* return point number */
  250.     }
  251.     return (curline + 1);    /* return point number */
  252. }
  253.